using System;
using System.Collections.Generic;
using System.Text;
using Mani.Security.Rules.AuthorizationDataTableAdapters;
using System.Web.Security;
using Mani.Security.Rules;

namespace Mani.Security {
      
    public class Action {

        private Guid id;

        private string name;

        private string description;

        private Action() { }

        public static Action Load( Guid id ) {
            using (aspnet_ActionsTableAdapter adapter = new aspnet_ActionsTableAdapter()) {
                AuthorizationData.aspnet_ActionsDataTable table = adapter.GetDataByActionId( id );
                if (table.Count == 0)
                    return null;

                Action action = new Action();
                action.name = table[0].ActionName;
                action.description = (table[0].IsDescriptionNull()) ? "" : table[0].Description;
                return action;
            }
        }

        public string Description {
            get { return description; }
            set { description = value; }
        }

        public string Name {
            get { return name; }
            set { name = value; }
        }	

        public Guid Id {
            get { return id; }
            set { id = value; }
        }
	
    }

    public interface IActionFactory {
        Guid Create( string name, string Description, object[] args, Guid applicationId );
    }

    public class SqlActionFactory : IActionFactory {
        public Guid Create( string name, string Description, object[] args, Guid applicationId ) {
            using (aspnet_ActionsTableAdapter adapter = new aspnet_ActionsTableAdapter()) {
                adapter.InsertAction( applicationId, name, name.ToLower(), Description );

                AuthorizationData.aspnet_ActionsRow row = adapter.GetDataByActionName( applicationId, name.ToLower() )[0];
                return row.ActionId;
            }
        }
    }
}
